package com.fyzk.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.fyzk.activity.lottery.togetherbuy.HeMaiGenDanActivity;
import com.fyzk.bean.TogetherBuyRecordItem;
import com.fyzk.dialog.SureDialog;
import com.tyjc.lottery.R;
import com.fyzk.net.PublicMethod;
import com.fyzk.utils.IntentParamUtils;
import com.fyzk.utils.LotteryIDUtils;
import com.fyzk.utils.Utils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;

public class UserHeMaiListAdapter extends BaseAdapter {

    private String TAG = this.getClass().getSimpleName();

    private ArrayList<TogetherBuyRecordItem> infors = new ArrayList<>();
    private Context context;
    // 日期
    private SimpleDateFormat format_date = new SimpleDateFormat(
            "yyyy-MM-dd HH:mm:ss");

    /**
     * 跟单提示对话框
     */
    private SureDialog genDanTiShiDialog = null;

    public UserHeMaiListAdapter(Context context, ArrayList<TogetherBuyRecordItem> infors) {
        this.context = context;
        this.infors = infors;
    }

    @Override
    public int getCount() {
        return infors.size();
    }

    private int isFollow;

    public void setIsFollow(int isFollow) {
        this.isFollow = isFollow;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return infors.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public void notifyDataSetChanged() {
        // TODO Auto-generated method stub
        super.notifyDataSetChanged();
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.item_togetherbuyrecord, null);
            convertView.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams
                    .MATCH_PARENT, Utils.dip2px(80)));
            holder.order_amount = (TextView) convertView.findViewById(R.id.order_amount);
            holder.order_lotteryName = (TextView) convertView.findViewById(R.id.order_lotteryName);
            holder.order_phase = (TextView) convertView.findViewById(R.id.order_phase);
            holder.order_sendtype = (TextView) convertView.findViewById(R.id.order_sendtype);
            holder.order_status = (TextView) convertView.findViewById(R.id.order_status);
            holder.fg_togetherbuyrecord_item = (FrameLayout) convertView.findViewById(R.id
                    .fg_togetherbuyrecord_item);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        TogetherBuyRecordItem togetherBuyRecordItem = infors.get(position);

        holder.order_lotteryName.setText(togetherBuyRecordItem.getLotteryTypeName());
        int buyType = togetherBuyRecordItem.getBuyType();
        if (buyType == 1) {
            holder.order_sendtype.setText("发起合买");
            holder.order_sendtype.setTextColor(context.getResources().getColor(R.color.top_bg));
        } else {
            holder.order_sendtype.setText("参与合买");
            holder.order_sendtype.setTextColor(context.getResources().getColor(R.color
                    .color_666666));
        }
        holder.order_amount.setText(PublicMethod.formatMoney(togetherBuyRecordItem.getTotalAmt())
                + "米     认购" + PublicMethod.formatMoney(togetherBuyRecordItem.getUserBuy()
        ) + "米");
        String displayState = togetherBuyRecordItem.getDisplayStateMemo();
        holder.order_status.setText(displayState);
        holder.order_phase.setText(togetherBuyRecordItem.getPhase() + "期");
        final String orderId = togetherBuyRecordItem.getCaseId();
        holder.fg_togetherbuyrecord_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toXiangXi(orderId);
            }
        });
//        String displayStateStr = "认购中";
//        switch (displayState){
//            case 1:
//                displayStateStr = "认购中";
//                holder.order_status.setText("方案状态："+displayStateStr);
//                break;
//            case 2:
//                displayStateStr = "已满员";
//                holder.order_status.setText("方案状态："+displayStateStr);
//                break;
//            case 3:
//                displayStateStr = "成功";
//                holder.order_status.setText("方案状态："+displayStateStr);
//                break;
//            case 4:
//                displayStateStr = "已撤单";
//                holder.order_status.setText("方案状态："+displayStateStr);
//                break;
//            case 5:
//                displayStateStr = "已流单";
//                holder.order_status.setText("方案状态："+displayStateStr);
//                break;
//            case 6:
//                displayStateStr = "已中奖";
//                holder.order_status.setText("方案状态："+displayStateStr);
//                break;
//            case 7:
//                displayStateStr = "出票失败";
//                holder.order_status.setText("方案状态："+displayStateStr);
//                break;
//        }

//		final HeMaiRecordInfor info;
//		info = jcInfors.get(position);
//		L.v(TAG, info.toString());
////		holder.faQi_ll.setVisibility(View.VISIBLE);
////		holder.faQi_tv.setValue(info.name);
//
//		holder.title1.setValue(info.time + "");
//		holder.title.setValue(context.getStringFromStream(AppLotteryInfos.getLotteryName(info
// .lotteryType)));
//		holder.title.setValue(info.lotteryName);
//		holder.wanFa.setValue(ConstantsDJ.wcode(info.lotteryType));
//		holder.time.setValue(info.time + "");
//		holder.state.setValue(ConstantsDJ.heMai_state(info.statusMemo));
//		holder.money.setValue("认购:" + info.buyAmount + "米");
//		if (info.prizeAmount != null && !info.prizeAmount.equals("")
//				&& !info.prizeAmount.equals("0")) {
//			holder.state.setTextColor(Color.RED);
//			holder.prizeMoney.setValue("中奖:" + info.prizeAmount + "米");
//		} else {
//			holder.state.setTextColor(Color.BLACK);
//			holder.prizeMoney.setValue("");
//		}
//
//		holder.bg.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				if (isToGenDan(info.lotteryType)) {
//					String pid = info.id + "";
//					toXiangXi(pid);
//				}
//			}
//		});

        return convertView;
    }

    /**
     * 订单详细页
     *
     * @param id
     */
    private void toXiangXi(final String id) {
        Intent intent = new Intent(context, HeMaiGenDanActivity.class);
        intent.putExtra(IntentParamUtils.HM_HALLDETAIL_ID, id);
        context.startActivity(intent);
    }

    private boolean isToGenDan(String lotteryId) {
        int id = 0;
        try {
            id = Integer.parseInt(lotteryId);
        } catch (Exception e) {
            // TODO: handle exception
        }

        return isToGenDan(id);
    }

    private boolean isToGenDan(int lotteryId) {
        switch (lotteryId) {
            case LotteryIDUtils.JLDXF_LOTTERY_ID_INT:
            case LotteryIDUtils.JLHH_LOTTERY_ID_INT:
            case LotteryIDUtils.JLRSF_LOTTERY_ID_INT:
            case LotteryIDUtils.JLSF_LOTTERY_ID_INT:
            case LotteryIDUtils.JLSFC_LOTTERY_ID_INT:
            case LotteryIDUtils.DCBF_LOTTERY_ID_INT:
            case LotteryIDUtils.DCBQC_LOTTERY_ID_INT:
            case LotteryIDUtils.DCSPF_LOTTERY_ID_INT:
            case LotteryIDUtils.DCSXDS_LOTTERY_ID_INT:
            case LotteryIDUtils.DCZJQ_LOTTERY_ID_INT:
                showTiShiDialog();
                return false;

            default:
                break;
        }

        return true;
    }


    class ViewHolder {
        TextView order_lotteryName;
        TextView order_phase;
        TextView userbuy;
        TextView order_sendtype;
        TextView order_amount;
        TextView order_status;
        FrameLayout fg_togetherbuyrecord_item;
    }

    /**
     * 忘记密码对话框
     */
    private void showTiShiDialog() {
//		genDanTiShiDialog.setTishiString1("");
//		genDanTiShiDialog.setTishiString2("手机端暂未开售此彩种，请到网站进行查看");
//		genDanTiShiDialog.getBtnSure().setValue(Html.fromHtml
// ("<a href=\"http://www.funyunzhongcheng.com\">GO>></a> "));
//		genDanTiShiDialog.getBtnSure().setMovementMethod(LinkMovementMethod.getInstance());
//		genDanTiShiDialog.setBtnCancelString("取消");
//		genDanTiShiDialog.setOnDialogOKCancleClickListener(new OnDialogOKCancleClickListener() {
//
//			@Override
//			public void onOKClick() {
//
//				genDanTiShiDialog.dismiss();
//			}
//
//			@Override
//			public void onCancleClick() {
//				// TODO Auto-generated method stub
//				genDanTiShiDialog.dismiss();
//			}
//		});
//		genDanTiShiDialog.show();
    }
}